package com.taoxi.good.controller;

import com.taoxi.common.annotation.Anonymous;
import com.taoxi.common.annotation.Log;
import com.taoxi.common.core.controller.BaseController;
import com.taoxi.common.core.domain.AjaxResult;
import com.taoxi.common.core.page.TableDataInfo;
import com.taoxi.common.enums.BusinessType;
import com.taoxi.common.utils.poi.ExcelUtil;
import com.taoxi.good.domain.Goods;
import com.taoxi.good.service.IGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * 商品管理Controller
 *
 * @author taoxi
 * @date 2024-10-28
 */
@Api(tags = "商品管理Controller")
@RestController
@RequestMapping("/good/goods")
public class GoodsController extends BaseController {
    @Autowired
    private IGoodsService goodsService;

    /**
     * 查询商品管理列表
     */
    @ApiOperation("查询商品管理列表")
    @PreAuthorize("@ss.hasPermi('good:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(Goods goods) {
        startPage();
        List<Goods> list = goodsService.selectGoodsList(goods);
        return getDataTable(list);
    }

    /**
     * 查询热卖商品列表
     */
    @Anonymous
    @ApiOperation("热卖商品")
    @GetMapping("/selectTop")
    public TableDataInfo selectTop(Goods goods) {
        startPage();
        List<Goods> list = goodsService.selectTopGoods(goods);
        return getDataTable(list);
    }

    /**
     * 查询店铺商品列表
     */
    @Anonymous
    @ApiOperation("查询店铺商品")
    @GetMapping("/selectSuppleGoods/{id}/{type}")
    public TableDataInfo selectSuppleGoods(@PathVariable("id") Long id, @PathVariable("type") String type) {
        startPage();
        List<Goods> list = goodsService.selectSuppleGoods(id, type);
        return getDataTable(list);
    }

    /**
     * 导出商品管理列表
     */
    @ApiOperation("导出商品管理列表")
    @PreAuthorize("@ss.hasPermi('good:goods:export')")
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods) {
        List<Goods> list = goodsService.selectGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<Goods>(Goods.class);
        util.exportExcel(response, list, "商品管理数据");
    }


    /**
     * 获取商品管理详细信息
     */
    @ApiOperation("获取商品管理详细信息")
    @PreAuthorize("@ss.hasPermi('good:goods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(goodsService.selectGoodsById(id));
    }

    /**
     * 新增商品管理
     */
    @ApiOperation("新增商品管理")
    @PreAuthorize("@ss.hasPermi('good:goods:add')")
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody Goods goods) {
        return toAjax(goodsService.insertGoods(goods));
    }

    /**
     * 修改商品管理
     */
    @ApiOperation("修改商品管理")
    @PreAuthorize("@ss.hasPermi('good:goods:edit')")
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Goods goods) {
        return toAjax(goodsService.updateGoods(goods));
    }

    /**
     * 删除商品管理
     */
    @ApiOperation("删除商品管理")
    @PreAuthorize("@ss.hasPermi('good:goods:remove')")
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(goodsService.deleteGoodsByIds(ids));
    }

//    /**
//     * 查询实体店销量排序
//     */
//    @Anonymous
//    @ApiOperation("实体店销量排序")
//    @GetMapping("/getSalesSort")
//    public TableDataInfo getSalesSort(Store store) {
//        startPage();
//        List<Store> list = goodsService.getSalesSort(store);
//        return getDataTable(list);
//    }
}

